package com.yc.damai.web;

import java.io.IOException;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.google.gson.Gson;
import com.yc.damai.bean.Result;
import com.yc.damai.bean.User;
import com.yc.damai.biz.BizException;
import com.yc.damai.biz.UserBiz;

@WebServlet("/user/*")
public class UserServlet extends BaseServlet {

	private static final long serialVersionUID = 1L;
	
	private UserBiz biz = new UserBiz();

	/**
	 *	请求地址  /user/login.s
	 */
	// 于 doGet 除了名字不同， 其他（参数，返回值类型。。。）必须都一样
	protected void login(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		String username = request.getParameter("username");
		String password = request.getParameter("password");		
		try {
			User user = biz.login(username, password);
			request.getSession().setAttribute("loginedUser", user);
			String json = new Gson().toJson(new Result(1,"登录成功"));
			response.getWriter().append(json);
		} catch (BizException e) {
			e.printStackTrace();
			String json = new Gson().toJson(new Result(0,e.getMessage()));
			response.getWriter().append(json);
		}
	}
	
	//注册方法   /user/reg.s  
	protected void reg(HttpServletRequest request, HttpServletResponse response) {
		
	}
	

}
